///<reference path="file://D:/Android/touch/sencha-touch-all-debug-w-comments.js" />

var extObjectsMap = Ext.create("Ext.util.HashMap");
Ext.namespace("keni.html");
keni.html.UIRender = function () {

    function createExtFields(fieldsMetadataArray) {
        var extFields = new Array();
        Ext.Array.each(fieldsMetadataArray, function (fieldMetadata, index, array) {
        	alert(fieldMetadata.fieldType);
        	try{
        		 var extField = Ext.create("Ext.field." + fieldMetadata.fieldType, { 
        			 fullscreen: true,
        		        renderTo: Ext.get('div2')});	

            for (property in fieldMetadata) {
            	alert(property);
                if (extField[property]) {
                	
                    extField[property] = fieldMetadata[property];
                }
            }
            extFields[index] = extField;
            extObjectsMap.add(fieldMetadata.htmlElementId, extField);
        	}
            catch(error)
            {
         	  alert(error); 
            }

        });
    }

    function createExtFieldSet(fieldSetMetadataArray) {

        Ext.Array.each(fieldSetMetadataArray, function (fieldSetMetadata, index, array) {
            var extFieldSet = Ext.create("Ext.form.FieldSet", {});
            for (property in fieldSetMetadata) {
                if (extFieldSet[property]) {
                    extFieldSet[property] = fieldSetMetadata[property];
                }
            }

            extFieldSet.add(createExtFields(fieldSetMetadata.fields));
            extObjectsMap.add(fieldSetMetadata.htmlElementId, extFieldSet);
        });

    }

    function createExtFormPanel(panelMetadataArray) {
        Ext.Array.each(panelMetadataArray, function (panelMetadata, index, array) {
            var extPanel = Ext.create("Ext.form.Panel");
            for (property in panelMetadata) {
                if (extPanel[property]) {
                    extPanel[property] = panelMetadata[property];
                }
            }
            extPanel.add(createExtFieldSet(panelMetadata.fieldSets));
            extPanel.add(createExtFields(panelMetadata.fields));
            extObjectsMap.add(panelMetadata.htmlElementId, extPanel);
        });
    }

    this.render = function (pageMetadata) {
      var pageMetadataObj = Ext.JSON.decode(pageMetadata);
      	alert(pageMetadata);
       // createExtFormPanel(pageMetadataObj.panels);
        createExtFields(pageMetadataObj.fields);
    }


}


